package com.hmh.student.students.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmh.student.students.entity.StudentCourse;
import com.hmh.student.students.service.ICourseService;
import com.hmh.student.students.service.IStudentCourseService;
import com.hmh.student.system.entity.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyf
 * @since 2025-04-16
 */
@RestController
@RequestMapping("/studentCourse")
public class StudentCourseController {
    @Autowired
    IStudentCourseService studentCourseService;
    @Autowired
    ICourseService courseService;

    // 修改学生课程信息
    @PutMapping("/update")
    public Map<String, Object> updateStudentCourse(@RequestBody StudentCourse studentCourse) {
        Map<String, Object> map = new HashMap<>();

        if (studentCourse.getCourseNo() == null || !courseService.existsById(studentCourse.getCourseNo())) {
            map.put("msg", "fail");
            map.put("error", "课程ID不存在或未提供");
            return map;
        }

        try {
            studentCourseService.updateById(studentCourse);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "更新失败: " + e.getMessage());
        }
        return map;
    }

    // 添加学生课程信息
    @PostMapping("/add")
    public Map<String, Object> addStudentCourse(@RequestBody StudentCourse studentCourse) {
        Map<String, Object> map = new HashMap<>();

        if (studentCourse.getCourseNo() ==null || !courseService.existsById(studentCourse.getCourseNo())){
            map.put("msg", "fail");
            map.put("error", "课程ID不存在或未提供");
            return map;
        }

        try {
            studentCourseService.save(studentCourse);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "添加失败");
        }
        return map;
    }

    // 获取单个用户角色信息
    @GetMapping("/{id}")
    public ResponseEntity<StudentCourse> getById(@PathVariable Integer id) {
        StudentCourse studentCourse = studentCourseService.getById(id);
        if (studentCourse == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(studentCourse);
    }

    // 删除学生课程信息
    @DeleteMapping("/delete")
    public Map<String, Object> deleteStudentCourse(@RequestParam Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            studentCourseService.removeById(id);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "删除失败");
        }
        return map;
    }

    // 分页查询学生课程信息
    @GetMapping("/page")
    public Page<StudentCourse> pageStudentCourse(
            @RequestParam(required = false) Integer studentNo,
            @RequestParam(required = false) Integer courseNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer current) {
        return studentCourseService.queryStudentCoursePage(studentNo, courseNo, pageSize, current);
    }


}
